package com.guodu.service.impl;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.guodu.dao.MaterialGhycDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.MaterialGhyc;
import com.guodu.entity.MaterialghycStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.MaterialGhycService;
import com.guodu.vo.GhycVo;
import com.guodu.vo.MaterialGhycQueryVo;
import com.guodu.vo.MaterialGhycVo;

@Service("materialGhycService")
public class MaterialGhycServiceImpl implements MaterialGhycService {

	@Resource
	private MaterialGhycDao materialGhycDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Override
	public List<MaterialGhycVo> findByVo(MaterialGhycQueryVo materialGhycQueryVo,Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		List<MaterialGhyc> list = materialGhycDao.findByVo(materialGhycQueryVo,units, curPage, pageSize);
		List<MaterialGhycVo> materialGhycVos = new ArrayList<MaterialGhycVo>();
		for(MaterialGhyc materialGhyc : list ){
			MaterialGhycVo materialGhycVo = new MaterialGhycVo(materialGhyc);
			UserInfo userInfo = userInfoDao.findById(materialGhyc.getCreatePeople());
			Units unitsInfo = unitsDao.findById(userInfo.getUnitsId());
			materialGhycVo.setUnitsName(unitsInfo.getUnitsName());
			materialGhycVo.setIndustry(unitsInfo.getIndustry());
			materialGhycVo.setUnitsId(unitsInfo.getUnitsId());
			if(unitsInfo.getFunitsId()!=null){
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				materialGhycVo.setFunitsName(funitsInfo.getUnitsName());
				materialGhycVo.setFunitsId(funitsInfo.getUnitsId());
			}
			materialGhycVo.setUserName(userInfo.getUserName());
			materialGhycVos.add(materialGhycVo);
		}
		return materialGhycVos;
	}

	@Override
	public int findSizeByVo(MaterialGhycQueryVo materialGhycQueryVo,Units units) {
		// TODO Auto-generated method stub
		return materialGhycDao.findSizeByVo(materialGhycQueryVo,units);
	}

	@Override
	public MaterialGhyc findById(String id) {
		// TODO Auto-generated method stub
		return materialGhycDao.findById(id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		materialGhycDao.del(id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generated method stub
		materialGhycDao.delByIds(ids);
	}

	@Override
	public void save(MaterialGhyc entity) {
		// TODO Auto-generated method stub
		materialGhycDao.save(entity);
	}

	@Override
	public void examine(String ids,String reason,String beforeStatus,Long afterStatus,UserInfo userInfo) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<MaterialghycStatus> entities = new ArrayList<MaterialghycStatus>();
		for(int i=0;i<idses.length;i++){
			MaterialghycStatus entity = new MaterialghycStatus();
			entity.setAfterStatus(afterStatus);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(userInfo.getUserId());
			entity.setMaterialId(idses[i]);
			entity.setReason(reason);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		materialGhycDao.changeStatus(entities);
		materialGhycDao.examine(ids,afterStatus);
	}

	@Override
	public void update(MaterialGhyc entity) {
		// TODO Auto-generated method stub
		materialGhycDao.update(entity);
	}

	@Override
	public List<MaterialghycStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return materialGhycDao.findStatus(id);
	}

	@Override
	public List<MaterialGhyc> findAllByVo(
			MaterialGhycQueryVo materialGhycQueryVo, Units units) {
		// TODO Auto-generated method stub
		return materialGhycDao.findAllByVo(materialGhycQueryVo, units);
	}

	@Override
	public GhycVo findAllMoney(MaterialGhycQueryVo materialGhycQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		GhycVo ghycVo = materialGhycDao.findAllMoney(materialGhycQueryVo, units);
		GhycVo ghycVo2 = materialGhycDao.findAllKcMoney(materialGhycQueryVo, units);
		ghycVo.setAllqckc(ghycVo2.getAllqckc());
		ghycVo.setAllqmkc(ghycVo2.getAllqmkc());
		return ghycVo;
	}

	@Override
	public boolean examinAll(MaterialGhycQueryVo materialGhycQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialGhycDao.examinAll(materialGhycQueryVo,units);
	}

	@Override
	public boolean allBack(MaterialGhycQueryVo materialGhycQueryVo, Units units) {
		// TODO Auto-generated method stub
		return materialGhycDao.allBack(materialGhycQueryVo,units);
	}

	@Override
	public boolean huituiAll(MaterialGhycQueryVo materialGhycQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialGhycDao.huituiAll(materialGhycQueryVo,units);
	}

	@Override
	public boolean shanchuAll(MaterialGhycQueryVo materialGhycQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialGhycDao.shanchuAll(materialGhycQueryVo,units);
	}



}
